BUILD_DIRECTORY := ../../../Build

XCODE_ACTION = build
XCODE_TARGET = Installer
XCODE_CONFIG = Release

$(BUILD_DIRECTORY)/Dist/PlugSuit.dmg : DiskImage/Template.dmg "$(BUILD_DIRECTORY)/$(XCODE_CONFIG)/PlugSuit Installer.app"
	mkdir -p $(BUILD_DIRECTORY)/Dist
	-rmdir $(BUILD_DIRECTORY)/MountPoint
	mkdir $(BUILD_DIRECTORY)/MountPoint
	-rm "$(BUILD_DIRECTORY)/Dist/__ShadowFile" 2>&1
	hdiutil attach \
		"$<" \
		-shadow "$(BUILD_DIRECTORY)/Dist/__ShadowFile" \
		-mountpoint "$(BUILD_DIRECTORY)/MountPoint" \
		-nobrowse
	cp -Rv "$(BUILD_DIRECTORY)/$(XCODE_CONFIG)/PlugSuit Installer.app" "$(BUILD_DIRECTORY)/MountPoint/"
	hdiutil detach "$(BUILD_DIRECTORY)/MountPoint"
	hdiutil convert \
		"$<" \
		-format UDZO \
		-o "$@.tmp.dmg" \
		-imagekey zlib-level=9 \
		-shadow "$(BUILD_DIRECTORY)/Dist/__ShadowFile"
	rm "$(BUILD_DIRECTORY)/Dist/__ShadowFile"
	-rm "$@"
	mv "$@.tmp.dmg" "$@"

all: xcode dmg

clean: dmg-clean
	xcodebuild -target "$(XCODE_TARGET)" -configuration "$(XCODE_CONFIG)" clean


xcode:
	xcodebuild -target "$(XCODE_TARGET)" -configuration "$(XCODE_CONFIG)" $(XCODE_ACTION)

dmg: $(BUILD_DIRECTORY)/Dist/PlugSuit.dmg

dmg-clean:
	-rm $(BUILD_DIRECTORY)/Dist/PlugSuit.dmg 2>&1
	-hdiutil detach "$(BUILD_DIRECTORY)/MountPoint"
	-rmdir $(BUILD_DIRECTORY)/MountPoint 2>&1

"$(BUILD_DIRECTORY)/$(XCODE_CONFIG)/PlugSuit Installer.app": xcode
	
.PHONY: all xcode dmg clean dmg-clean
